package org.aeon.colorfulplanebattle.view.components;

import org.aeon.colorfulplanebattle.view.GameState;
import org.aeon.colorfulplanebattle.view.logic.FlyDrawTask;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.BATTLEFIELD_HEIGHT;
import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.BATTLEFIELD_WIDTH;

/**
 * @author: MissingAeon
 * @date: 2024/1/9 10:01
 * @description:
 */
public class Battlefield extends JComponent {
    private static final BufferedImage image;

    static {
        InputStream resource = Battlefield.class.getClassLoader().getResourceAsStream("");
        BufferedImage read = null;
        if (resource != null) {
            try {
                read = ImageIO.read(resource);
            } catch (IOException e) {
            }
        }
        image = read;
    }

    private final GameState gameState;
    private FlyDrawTask flyDrawTask;

    public Battlefield(GameState gameState) {
        this.gameState = gameState;
        this.setBounds(0, 0, BATTLEFIELD_WIDTH, BATTLEFIELD_HEIGHT);
        this.gameState.put("Battlefield", this);
        gameState.put("BattlefieldReset", (Runnable) this::reset);
    }

    private void reset() {
        if (gameState.exist("flyCtrl")) {
            flyDrawTask = gameState.get("flyCtrl");
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        if (flyDrawTask != null) {
            flyDrawTask.drawAll(g);
        }
    }
}
